All right.
So now that we created our landmines using random numbers on a flat terrain, how do we solve that problem
if we have variable terrain like hills and water and things like that?
I'm going to do that today with my health pick ups.
So let's move away from our sign where our flat landmine field is and make some variable terrain.
Let me open my terrain editor here.
We're not going to spend too much time, but let's just put a hill in here and maybe some water.
So with grass selected, I'm going to go ahead and grow that.
Make that a little bit bigger.
Strength is fine.
Let me turn my plane lock off.
I do have it off.
All right.
There we go.
Oh, man.
I went to Stone Grass.
There we go.
We just got right over it.
So we've got this hill going here.
That's your work.
A little bit of rock in there.
Maybe we'll put some rock in there.
Let's get some rock.
Maybe make this thing smaller.
Get a little Machu Picchu thing going.
Cool.
Maybe some water, too.
Let's go to paint.
Go down to water and I'm going to make that a little bigger.
Here we go.
Got a lake.
They make this one a little small here.
Cool.
All right, Now that we got some, we got some unusual terrain, let's go ahead and put a center point
here so we make sure that our health pick ups are over the funny terrain just to make sure it works.
You could you could expand this out to the whole terrain.
But I want to make sure that we have some some good test area.
So I'm going to add a block right here.
There we go.
That's pretty good.
That's centered.
Let's just call this like health center.
Health center.
Nice.
Going to be a little reference point.
I'm going to make it invisible.
Let's see.
Turn can collide off.
So don't trip over it.
Turn anchored on so it doesn't fall down through the ground.
And then let's scroll all the way up to where it says transparency.
Make that one.
Let's go down to server storage and take a quick look at our health pickup.
We have a health pick up here.
I'm going to do a control D to duplicate it.
I'm going to drag it up into our workspace so we can see it.
All right.
So it's really far away.
I can't even see it way over there.
Let's just go ahead and get our health center and then I will do a control C on the position under C
frame for the health center.
And then I want to go to my health pickup and then under C frame position control V boom, there it
is.
It's in the ground.
I'm going to lift that up a little bit.
Now I want this to be floating about three studs above the ground and I want to be able to randomly
select terrain.
But we don't know the height of the terrain and we also want it to be three studs above the water.
If we pick an X and a Z out here, let's go ahead and get started with that using a technique called
ray casting.
Let's go ahead and delete this example.
Health pickup.
Right click, delete.
And we're going to open our session utils.
I have it open right here, but if you don't have it, it's under server or server script service right
there.
You double click and open it up.
All right, now we're going to add a helper function, let's say a local function, and I'm going to
call this cast ray down for height.
I want to determine the height of the terrain, given an X and a Z, Right.
So the X and the Z is the is the flat part of the map and the Y is the height.
So I have my X, I need a Y and I have a Z.
Let's get a variable called origin.
And origin is going to be the origin of the ray.
So I'll say origin equals vector three new.
Well, we have our X and our Z, we need a Y, and I'm going to go with 300 studs above my terrain.
I know my hills aren't going to be higher than 300 studs.
If you have high hills, go ahead and increase that number.
All right.
I'm going to need a stop point on my ray and I want to make sure I go past the terrain.
I'm going to be looking down.
I'm going to say vector three new.
I know my X, I'm going to go -20 on the Y.
You could go past that if you want.
But I know I'm not going more than -20 in any of my terrain areas and I have my same Z.
We're going to look straight down.
I need another variable.
I'm going to call it deer for direction.
I'm going to get the stop minus the origin.
All right.
I know that's a little more than direction.
Right?
Direction would just be a unit vector, but that's fine if you don't know what that is, that's fine,
because we're not going to care.
So I'm going to get this result.
If I do my workspace, Colon ray cast, I'm going to start with the origin.
See, it tells you right here Cas array using the origin direction and optional ray cast parameters.
We're not going to use ray cast parameters, but that gives you cool things like an ignore list.
Like if you want to ignore the water or you want to ignore certain materials, we're going to do that
when we're using our guns, when we're making guns.
So let's say origin, I need my direction, which is a little more than direction.
It's direction and distance and then nil for our parameters, or you can put nothing there to pass it
in.
Right.
So this will work just fine.
That'll pass in a nil for the parameters.
But I'm going to put this here just so that we know that something could go there.
All right?
If we hit something with our array, like the ground.
Right, we're going to get a result.
So we have to check if result then.
All right.
Well, if we hit the ground, luckily we have something in our result called position that gives me
an X, a Y, and Z at the point of impact of the ray.
Right.
The ray is invisible.
You can't see it.
But now we know where our height is because we have our y value.
We already know the X and z, but we did not know the Y.
So I'm going to do my health pick ups very similar to how I did my landmines.
So I'm going to need a folder much like these, like this mine folder and zombie folder.
Let's go to our workspace, hit the plus, add a folder and I'm going to put a call this health, right?
Maybe health pickups.
Now, health is fine health and we'll go and get a variable for that in our session.
Utils.
Local health Folder.
Workspace Health.
All righty.
Let's go down to where our minds are.
This is going to be very, very similar.
We'll get a function and we're going to call this from our wave loop.
So it's not going to be local utils place health.
Right?
I'm going to get the number of health and this is going to be similar to the minds.
We're going to have a certain number of health starting out at the beginning of every round.
I'm going to get my center point because I'm not going to spread them throughout the whole world specifically
because I want to check my terrain stuff right now.
And then I'm going to have a distance from my center point.
Now, what we want to do is make a template object of our health pickup.
Remember, in server storage, we have a health pickup.
Make sure it's there and I'll call this PT Health.
That's my template.
Health.
I'm going to get that from server storage server storage weight from child health pickup.
And then I'm just going to clone from there.
I'm also going to get the count of health that are in my health folder.
So I know if I need to make more or how many I need to make health folder Got children.
So I use this little pound sign in front of the health folder colon.
Get children to get the number of elements in that folder.
Now I'm going to do a while loop before down here I did a for loop.
I thought it'd be kind of cool to change it up.
So you see, there's different ways going to say while count is less than the number of health's do.
Let's get our X and Z random numbers right.
So I have an x rand and we have a random number generator.
Next integer.
We are going to go minus distance to distance.
So if I put 50 in there for distance, we have plus plus 52 -50.
Let's do one for the z and brand next integer and I'll do distance two or negative distance the distance.
Now we need to get a position and we also need to have our y value.
So I'm going to say cast Ray down from height.
I'm going to put in my center point dot x plus my x rand and my center point dot Z plus Z, and that's
going to give me my location on the map centered around my center point, which is going to be, I think
I call that health center, but we didn't pass that in yet.
We're going to pass that in from our wave loop.
All right.
And I'm going to get a Y back with this position.
Remember, I'm going to get a whole X, Y, and Z when I get when when the the array collides with the
earth over the X in the Z location.
So I need to move my health three studs up from the earth or the terrain, whatever you want to call
it, I'm going to get this position and I'm going to add three studs to it.
So I'm going to take the old position and add to a new position plus three studs.
I'll do that plus equals factor three new zero.
We're not going to touch the x three, we're going to increase the Y zero, we're not going to touch
the Z.
So that's going to pick up my position by three studs.
Now let's go ahead and get our health by cloning HT health, and then we'll make the parent let's do
this, like make the parent of the health, the health folder, which is going to put the health object
in the health folder.
We'll get the health position.
So we're just going to use position.
We're not going to use move to because it's a single object.
So we could just change the position to pose, which is three studs above the earth on our random X
and Z plus or minus the center location.
We have a debris service which will give our our health a time to live.
Let's do that.
We'll save 5 minutes just to change things around a little bit in case we get one we can't reach or
something.
And then we're going to get a new value for our count.
Right.
Because the health folder is going to have changed.
So just do pound sign health folder, get children.
We're going to come back up here.
We're going to do our check.
If it's if it's not enough, we're just going to go through and continue until we have enough health.
We still need to call this place health from our wave loop.
Let's go ahead and copy it so we don't forget what it is.
Control C.
Go to wave loop, which is also in server script service right there.
Wave loop.
I'm going to go.
Maybe I'll do it above my place.
Mine's.
Let's go ahead and do a control V and I'll just put a weight in there.
So we need a number of health's let's get variables for these local number of health's.
And how many should we get it?
Give it 2020, and then distance from center right.
So we need to get our center point for the health's.
I think I'm going to call this something different.
I'm going to call this health center POS because I don't want it to be too vague here.
How are we going to do that?
We'll do workspace and then I think I have a health center.
There we go.
And then there's a position associated with that that's good and change this control C, Control V and
then we need a distance from the health center, distance from health center, maybe 100, maybe let's
do 50 for our example though, because we want we want it to get a lot of health around our our strange
terrain to see if our recasting is working.
So let's say distance from health center.
Cool.
Let's go to our world and try it out.
I want to do a play from here, though.
I don't want to have to run around, so I'm going to hit this little arrow play here.
And any second we're going to hear eruption.
Look at that.
Three studs high.
So we have this variable terrain.
We're able to calculate out the height of it and then place our pickups appropriately, even over the
water.
And you put water in the ignore list for the ray casting, but we don't have that.
And I didn't want that.
I'd want I wanted it over the water.
That's pretty cool.
So I will see you in the next video.